/****************************************************************************
 * Copyright (c) 2022 Qualcomm Technologies International, Ltd.
****************************************************************************/
/**
 * \defgroup noise_id
 * \ingroup capabilities
 * \file  noise_id.h
 *
 * Noise ID (NOISE_ID) operator public header file.
 *
 */

#ifndef _NOISE_ID_H_
#define _NOISE_ID_H_

/******************************************************************************
Include Files
*/
#include "types.h"
#include "audio_log/audio_log.h"

/* Imports scratch memory definitions */
#include "mem_utils/scratch_memory.h"

/* Math FFT interface */
#include "math/fft_twiddle_alloc_c_stubs.h"

#include "anc_hw_manager_event.h"
#include "aanc_security_public.h"       /* AANC security library */
#include "base_aud_cur_op.h"            /* Class functions */
#include "noiseid100_struct_public.h"   /* Library definitions */
#include "noiseid100_public.h"          /* Library functions */
#include "ed100_public.h"               /* Energy detector library */

#include "noise_id_gen_c.h"             /* Autogenerated NOISE ID definitions */
#include "noise_id_defs.h"              /* Autogenerated NOISE ID definitions */

/****************************************************************************
Public Variable Definitions
*/

extern const CAPABILITY_DATA noise_id_cap_data; /* Capability data */

/******************************************************************************
Private Constant Definitions
*/

/* Number of statistics reported by the capability */
#define NOISE_ID_N_STAT                  (sizeof(NOISE_ID_STATISTICS)/sizeof(ParamType))

/* Mask for the number of system modes */
#define NOISE_ID_SYSMODE_MASK            0x7

/* Mask for override control word */
#define NOISE_ID_OVERRIDE_MODE_MASK      (0xFFFF ^ NOISE_ID_CONTROL_MODE_OVERRIDE)

#define NOISE_ID_MIN_VALID_SINKS         ((1 << NOISE_ID_FF_TERMINAL))
#define NOISE_ID_MIN_VALID_SOURCES       0

/* Terminals to skip data propagation in base class */
#define NOISE_ID_TERMINAL_SKIP_MASK      0

#define NOISE_ID_SUPPORTS_IN_PLACE       TRUE
#define NOISE_ID_SUPPORTS_METADATA       TRUE
#define NOISE_ID_DYNAMIC_BUFFERS         FALSE

/* Capability minor version. Capability versions:
 *
 * Version | Description
 * --------|--------------------
 *   1.0   | Initial Release
 *   1.1   | Introduce level based hysterisis using two thresholds.
 */
#define NOISE_ID_CAP_VERSION_MINOR       1

/* ED attack and decay time (s) */
#define NOISE_ID_ED_ATTACK               0x07FEF9DBu /* 3.998, Q7.N */
#define NOISE_ID_ED_DECAY                0x07FEF9DBu /* 3.998, Q7.N */

/* ED initial frame time (s) */
#define NOISE_ID_ED_INIT_FRAME           0x00020C4Au /* 0.004, Q7.N */

/* ED count threshold time (s) */
#define NOISE_ID_ED_COUNT_TH             0x010A3D71u /* 0.52, Q7.N */

/******************************************************************************
Public Enums
*/

typedef enum
{
    NOISE_ID_UNUSED_TERMINAL,            /* 0 */
    NOISE_ID_FF_TERMINAL,                /* 1 */
    NOISE_ID_PASSTHROUGH_TERMINAL,       /* 2 */
    NOISE_ID_MAX_TERMINALS               /* 3 */
} NOISE_ID_TERMINALS;

/******************************************************************************
Public Type Declarations
*/

/* NOISE_ID operator data */
typedef struct noise_id_exop
{
    unsigned sample_rate;               /* Operator sample rate */
    CAP_ID cap_id;                      /* Capability ID */
    NOISE_ID_PARAMETERS noise_id_params;  /* Operator parameters */

    unsigned cur_mode;                  /* Current mode */
    unsigned ovr_control;               /* Override status */
    unsigned host_mode;                 /* Mode setting from apps/host */
    unsigned qact_mode;                 /* Mode setting from QACT */

    void *f_handle;                     /* Feature handle for licensing */
    bool scratch_registered:8;          /* Track scratch registration */
    bool twiddle_registered:8;          /* Track FFT twiddle registration */

    tCbuffer *p_ff_mic;                 /* FF mic buffer pointer */
    NOISEID100_DMX *p_noise_id100;      /* Pointer to noise_id100 module */
    uint8 *p_noise_id100_dm1;           /* DM1 memory for noise_id100 */
    AANC_AFB *p_afb;                    /* Pointer to AFB */

    ED100_DMX *p_ed;                    /* Pointer to ED object */
    uint8 *p_ed_dm1;                    /* Pointer to ED DM1 memory */
    tCbuffer *p_tmp_ed;                 /* Pointer to temp buffer used by ED */

    unsigned previous_nid;              /* Previous noiseID */

    unsigned flags;                     /* Capability flags */
} NOISE_ID_OP_DATA;

/******************************************************************************
Private Function Definitions
*/

/* Standard Capability API handlers */
extern bool noise_id_create(OPERATOR_DATA *op_data,
                            void *message_data,
                            unsigned *response_id,
                            void **resp_data);
extern bool noise_id_destroy(OPERATOR_DATA *op_data,
                             void *message_data,
                             unsigned *response_id,
                             void **resp_data);

/* Standard Opmsg handlers */
extern bool noise_id_opmsg_set_control(OPERATOR_DATA *op_data,
                                       void *message_data,
                                       unsigned *resp_length,
                                       OP_OPMSG_RSP_PAYLOAD **resp_data);
extern bool noise_id_opmsg_get_status(OPERATOR_DATA *op_data,
                                      void *message_data,
                                      unsigned *resp_length,
                                      OP_OPMSG_RSP_PAYLOAD **resp_data);
extern bool noise_id_opmsg_set_current_noise_id(OPERATOR_DATA *op_data,
                                      void *message_data,
                                      unsigned *resp_length,
                                      OP_OPMSG_RSP_PAYLOAD **resp_data);
/* Standard data processing function */
extern void noise_id_process_data(OPERATOR_DATA *op_data,
                                  TOUCHED_TERMINALS *touched);

/* Capability hook functions to work with base_aud_cur_op */
bool noise_id_connect_hook(OPERATOR_DATA *op_data,
                           unsigned terminal_id);
bool noise_id_disconnect_hook(OPERATOR_DATA *op_data,
                              unsigned terminal_id);

#endif /* _NOISE_ID_H_ */
